package com.visikard.ui.fragment;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.visikard.application.Constant;
import com.visikard.application.UIMessage;
import com.visikard.application.VKRegApplication;
import com.visikard.asynctask.BaseTask;
import com.visikard.asynctask.SendAudienceToWalletTask;
import com.visikard.item.ItemCouponsBuilder;
import com.visikard.system.Device;
import com.visikard.ui.R;
import com.visikard.ui.activity.ChooseAudienceKard;
import com.visikard.ui.activity.CouponsRTUIMainActivity;
import com.visikard.utility.BitmapUtils;
import com.visikard.utility.UILImageUtils;

public class GiveCouponToAudience extends BaseFragment {

	public static final String MSG_SEND_ITEM_COUPONS = "SendItemCoupons";

	ItemCouponsBuilder itemCoupons;

	private ImageView imageCoupon;
	private TextView nameCoupon, percentOff, expireDate;
	// private ViewGroup ratingGroup;
	private ImageView btnBack, btnSearch;
	private Button btnGive, btnBroadcast, btnSendWallet;

	private long idCampaignSpecail = -1;
	private String listfkUser = "";
	private String listfkKard = "";
	private int isBroadcast = 0;
	private String couponName = "";

	DateFormat timeFormat;
	DateFormat formatter;

	private BaseTask mAsyncTask;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.give_coupon_to_audience, container, false);
		Bundle bundle = getArguments();
		if (bundle != null) {
			itemCoupons = (ItemCouponsBuilder) bundle.getSerializable(MSG_SEND_ITEM_COUPONS);
		}
		formatter = new SimpleDateFormat(Constant.TIME_FORMAT_STR, Locale.US);
		timeFormat = new SimpleDateFormat(Constant.TIME_FORMAT_STR_US_DATE, Locale.US);
		innitControl(view);
		return view;
	}

	private void innitControl(View view) {
		imageCoupon = (ImageView) view.findViewById(R.id.coupon_image);
		nameCoupon = (TextView) view.findViewById(R.id.name);
		percentOff = (TextView) view.findViewById(R.id.percent_off);
		expireDate = (TextView) view.findViewById(R.id.expire_date);
		// ratingGroup = (ViewGroup) view.findViewById(R.id.rating_bar);
		btnBack = (ImageView) view.findViewById(R.id.give_back_btn_menu);
		btnSearch = (ImageView) view.findViewById(R.id.search_btn_menu);
		btnGive = (Button) view.findViewById(R.id.btn_choose_to_give);
		btnBroadcast = (Button) view.findViewById(R.id.btn_broadcast);
		btnSendWallet = (Button) view.findViewById(R.id.btn_send_to_audience_wallet);

		if (itemCoupons != null) {
			LinearLayout.LayoutParams lyKard = (LinearLayout.LayoutParams) imageCoupon.getLayoutParams();
			lyKard.width = Device.getDeviceWidth();
			lyKard.height = Device.getDeviceWidth() / 2;

			couponName = itemCoupons.getCouponName();
			idCampaignSpecail = itemCoupons.getIdCampaignSpecail();
			nameCoupon.setText(itemCoupons.getCouponName());
			percentOff.setText(itemCoupons.getTextImage1());
			String strDate = getActivity().getResources().getString(R.string.coupons_tab_expire_date);
			expireDate.setText(strDate);
			// ImageLoader.getInstance(getActivity()).loadImageCorner(imageCoupon, itemCoupons.getImageSpecial(),
			// Device.getDeviceWidth(), BitmapUtils.ROUND_NONE, 0, 0, 0, 0, 0);
			UILImageUtils.loadImage(imageCoupon, // image view
					itemCoupons.getImageSpecial(), // url
					0, // size of view
					BitmapUtils.ROUND_NONE, // round all
					0, // round size
					0, // shadow
					0, // shadow resource
					0, 0);

			expireDate.setText(getResources().getString(R.string.coupons_tab_expire_date) + " "
					+ itemCoupons.getExpireDate());
		}
		btnBack.setOnClickListener(onclick);
		btnSearch.setOnClickListener(onclick);
		btnGive.setOnClickListener(onclick);
		btnBroadcast.setOnClickListener(onclick);
		btnSendWallet.setOnClickListener(onclick);
	}

	OnClickListener onclick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.give_back_btn_menu:
					((CouponsRTUIMainActivity) getActivity()).onBackPressed();
					break;
				case R.id.search_btn_menu:

					break;
				case R.id.btn_choose_to_give:
					Intent intent = new Intent(getActivity(), ChooseAudienceKard.class);
					startActivityForResult(intent, ChooseAudienceKard.FOR_RESULT_KEY);
					break;
				case R.id.btn_broadcast:
					isBroadcast = 1;
					displayConfirmSendCoupon();
					break;
				case R.id.btn_send_to_audience_wallet:
					if (!listfkUser.equals("") && !listfkKard.equals("")) {
						isBroadcast = 0;
						sendAudienceWallet(false);
					}
					else
						mHandler.sendEmptyMessage(UIMessage.MSG_CHOSSE_LIST_AUDIENCE_NOT_YET);

					break;
				default:
					break;
			}
		}
	};

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {
			listfkUser = data.getStringExtra(ChooseAudienceKard.MSG_RESULT_LIST_FKUSER);
			listfkKard = data.getStringExtra(ChooseAudienceKard.MSG_RESULT_LIST_FKKARD);
		}
	};

	private void displayConfirmSendCoupon() {

		AlertDialog mYesNoDialog = new AlertDialog.Builder(getActivity()).create();

		mYesNoDialog.setMessage(couponName + " " + getActivity().getString(R.string.alert_dilg_coupon_to_wallet_all));
		mYesNoDialog.setButton(DialogInterface.BUTTON_POSITIVE, getActivity().getString(android.R.string.yes),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						sendAudienceWallet(true);
					}
				});
		mYesNoDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getActivity().getString(android.R.string.cancel),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// Nothing
					}
				});
		mYesNoDialog.show();
	}

	private void sendAudienceWallet(final boolean isBroadCast) {
		mAsyncTask = new SendAudienceToWalletTask(mHandler);
		if (isBroadCast)
			mAsyncTask.execute(idCampaignSpecail, "", "", isBroadcast);
		else
			mAsyncTask.execute(VKRegApplication.uid, idCampaignSpecail, listfkUser, listfkKard, isBroadcast);
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
			case UIMessage.MSG_SEND_TO_WALLET_SUCCESS:
				Toast.makeText(
						getActivity(),
						couponName + " "
								+ getActivity().getResources().getString(R.string.alert_send_coupon_to_wallet_success),
						Toast.LENGTH_LONG).show();
				return true;
			case UIMessage.MSG_SEND_TO_WALLET_FAILED:
				Toast.makeText(
						getActivity(),
						couponName + " "
								+ getActivity().getResources().getString(R.string.alert_send_coupon_to_wallet_failed),
						Toast.LENGTH_LONG).show();
				return true;
			case UIMessage.MSG_CHOSSE_LIST_AUDIENCE_NOT_YET:
				Toast.makeText(getActivity(),
						getActivity().getResources().getString(R.string.alert_choose_audience_not_yet),
						Toast.LENGTH_LONG).show();
				return true;
		}
		return super.handleMessage(msg);
	}
}
